
import java.io.InputStream;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VolumeControl;

public class SoundPlay
        implements PlayerListener {

    public static final int sound1 = 0;
    public static final int sound2 = 1;
    public static final int sound3 = 2;
    public static final int sound4 = 3;
    public static final int sound5 = 4;
    public static final int sound6 = 5;
    public static final int sound7 = 6;
    public static final int sound8 = 7;
    public static final int sound9 = 8;
    public static final int sound10 = 9;
    public static final int sound11 = 10;
    public static final int sound12 = 11;
    public static final int sound13 = 12;
    public static final int sound14 = 13;
    public static final int sound15 = 14;
    public static final int sound16 = 15;
    public static final int[] sound = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
        12, 13, 14, 15};
    public static final String AUDIO_FORMAT = "audio/mpeg";
    private Player player;

    public SoundPlay(String path) {
        createPlayer(path);
    }

    public void play() {
        try {
            this.player.start();
        } catch (MediaException e) {
            e.printStackTrace();
        }
    }

    private void createPlayer(String path) {
        try {
            if (this.player != null) {
                this.player.close();
                this.player = null;
            }
            InputStream is = null;
            is = getClass().getResourceAsStream(path);
            this.player = Manager.createPlayer(is,
                    "audio/mpeg");
            this.player.realize();
            VolumeControl v = (VolumeControl) this.player
                    .getControl("VolumeControl");
            v.setLevel(95);
            this.player.prefetch();
            this.player.addPlayerListener(this);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void stop() {
        try {
            this.player.stop();
        } catch (MediaException e) {
            e.printStackTrace();
        }
    }

    public void playerUpdate(Player player, String event, Object eventData) {
        if (event.equals("endOfMedia")) {
            player.close();
        }
    }
}
